package com.swanclouds.redfish.account;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.swanclouds.api.Apis;
import com.swanclouds.api.account.AccountService;
import com.swanclouds.api.account.LdapService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.account.*;
import com.swanclouds.model.account.builder.BMC.BMCCreateAccount;
import com.swanclouds.model.account.builder.HuaWei.HuaWeiCreateAccount;
import com.swanclouds.model.common.ActionResponse;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.redfish.account.domain.AccountModel;
import com.swanclouds.redfish.account.domain.AccountResourceModel;
import com.swanclouds.redfish.account.domain.IBMCUserGroupModel;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseAccountService;

import java.util.ArrayList;
import java.util.List;

/**
 * AccountService 资源操作
 *
 * @author 张立强
 */
public class AccountServiceImpl extends BaseAccountService implements AccountService {

    /**
     * 查询用户服务信息
     *
     * @return AccountResource
     */
    @Override
    public AccountResource get() {
        return get(AccountResourceModel.class,uri(ClientConstants.PATH_ACCOUNTSERVICE)).execute();
    }

    /**
     * 获取用户集合资源信息
     * @return CollectionResource
     */
    @Override
    public CollectionResource getAccounts() {
        AccountResource accountResource = get();
        return get(CollectionResourceModel.class,uri(accountResource.accounts())).execute();
    }

    /**
     * 获取服务器指定用户信息
     *
     * @param accountId 用户ID
     * @return Account
     */
    @Override
    public Account getAccount(String accountId) {
        Preconditions.checkNotNull(accountId, "Account id is null");
        return get(AccountModel.class,uri(ClientConstants.ACCOUNTSERVICE_ACCOUNTS_ID, accountId)).execute();
    }

    /**
     * 获取服务器用户信息列表
     *
     * @return List&lt;Account&gt; accountList
     */
    @Override
    public List<Account> getAccountList() {
        List<Account> accountList = new ArrayList<>();
        CollectionResource accounts = getAccounts();
        accounts.getCollectionResourceList().stream().forEach(ref -> {
            accountList.add(get(AccountModel.class, uri(ref)).execute());
        });
        return accountList;
    }

    /**
     * 创建用户
     *
     * @param account 用户
     * @return ActionResponse
     */
    @Override
    public Account create(CreateAccount account) {
        Preconditions.checkNotNull(account, "account is null");
        Preconditions.checkNotNull(account.getUserName(), "UserName is null");
        Preconditions.checkNotNull(account.getPassword() ,"Password is null");
        Preconditions.checkNotNull(account.getRoleId(), "RoleId is null");
        if (account instanceof BMCCreateAccount) {
            Preconditions.checkNotNull(((BMCCreateAccount) account).getEnabled(), "Enabled is null");
            Preconditions.checkNotNull(((BMCCreateAccount) account).getLocked(), "Locked is null");
        } else if (account instanceof HuaWeiCreateAccount) {

        }
        return post(AccountModel.class,uri(ClientConstants.ACCOUNTSERVICE_ACCOUNTS)).entity(account).execute();
    }

    /**
     * 删除用户
     *
     * @param accountId 用户ID
     * @return ActionResponse
     */
    @Override
    public ActionResponse delete(String accountId) {
        Preconditions.checkNotNull(accountId, "Account Id is null");
        return deleteWithResponse(uri(ClientConstants.ACCOUNTSERVICE_ACCOUNTS_ID, accountId)).execute();
    }

    /**
     * 更新用户
     * @param account 用户信息
     * @param accountId 用户iID
     * @return ActionResponse
     */
    @Override
    public Account update(UpdateAccount account,String accountId) {
        Preconditions.checkNotNull(accountId, "Account Id is null");
        Preconditions.checkNotNull(account, "Account is null");
        return patch(AccountModel.class, uri(ClientConstants.ACCOUNTSERVICE_ACCOUNTS_ID,accountId)).entity(account).execute();
    }

    /**
     * 查询用户组集合信息
     *
     * @return CollectionResource
     */
    @Override
    public CollectionResource getRoles() {
        return get(CollectionResourceModel.class,uri(ClientConstants.ACCOUNTSERVICE_ROLES)).execute();
    }

    /**
     * 查询用户组信息
     *
     * @param roleId 用户组ID(角色ID)
     * @return IBMCRole
     */
    @Override
    public IBMCUserGroup getRole(String roleId) {
        Preconditions.checkNotNull(roleId, "UserGroup Id is null");
        return get(IBMCUserGroupModel.class, uri(ClientConstants.ACCOUNTSERVICE_ROLES_ID, roleId)).execute();
    }

    /**
     * 查询用户组信息列表
     *
     * @return java.util.List&lt;IBMCRole&gt;
     */
    @Override
    public List<IBMCUserGroup> getRoleList() {
        List<IBMCUserGroup> roleList = Lists.newArrayList();
        CollectionResource roles = getRoles();
        roles.getCollectionResourceList().stream().forEach(ref -> {
            roleList.add(get(IBMCUserGroupModel.class,uri(ref)).execute());
        });
        return roleList;
    }

    /**
     * 创建用户组信息
     *
     * @param userGroup 用户组
     * @return IBMCRole
     */
    @Override
    public IBMCUserGroup createRole(UserGroup userGroup) {
        Preconditions.checkNotNull(userGroup, "UserGroup is null");
//        Preconditions.checkNotNull(userGroup.getName(), "Name is null");
//        Preconditions.checkNotNull(userGroup.getPrivilege(), "Privileges is null");
        return post(IBMCUserGroupModel.class,uri(ClientConstants.ACCOUNTSERVICE_ROLES)).entity(userGroup).execute();
    }

    /**
     * 更新用户组信息
     *
     * @param userGroup 用户组
     * @param roleId 用户组ID(角色ID)
     * @return IBMCRole
     */
    @Override
    public IBMCUserGroup updateRole(UserGroup userGroup, String roleId) {
        Preconditions.checkNotNull(roleId, "UserGroup Id is null");
        Preconditions.checkNotNull(userGroup, "UserGroup is null");
//        Preconditions.checkNotNull(userGroup.getAssignedPrivileges(), "AssignedPrivileges is null");

        return patch(IBMCUserGroupModel.class,uri(ClientConstants.ACCOUNTSERVICE_ROLES_ID, roleId)).entity(userGroup).execute();
    }

    /**
     * 删除用户组信息
     *
     * @param roleId 用户组ID(角色ID)
     * @return ActionResponse
     */
    @Override
    public ActionResponse deleteRole(String roleId) {
        Preconditions.checkNotNull(roleId, "UserGroup Id is null");
        return deleteWithResponse(uri(ClientConstants.ACCOUNTSERVICE_ROLES_ID,roleId)).execute();
    }

    @Override
    public LdapService Ldap() {
        return Apis.get(LdapService.class);
    }


}
